package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import model.Game;

public class GameDao
{
	private static EntityManager em;

	private static EntityManager getEntityManeger() 
	{
		if (em == null || em.isOpen() == false) 
		{
			EntityManagerFactory emf = Persistence
					.createEntityManagerFactory("sk");
			em = emf.createEntityManager();
			return em;
		} else
			return em;
	}

	public static List<Game> getAllGames()
	{
		em = getEntityManeger();
		List<Game> allGames = null;

		TypedQuery<Game> getGamesQuery = em
				.createQuery("FROM Game", Game.class);
		allGames = getGamesQuery.getResultList();
		return allGames;
	}
	
	public static List<Game> getAllOpenedGames()
	{
		em = getEntityManeger();
		List<Game> allGames = null;

		TypedQuery<Game> getGamesQuery = em
				.createQuery("FROM Game WHERE endTime IS NULL", Game.class);
		allGames = getGamesQuery.getResultList();
		return allGames;
	}
	
	public static List<Game> getAllClosedGames()
	{
		em = getEntityManeger();
		List<Game> allGames = null;

		TypedQuery<Game> getGamesQuery = em
				.createQuery("FROM Game WHERE endTime IS NOT NULL", Game.class);
		allGames = getGamesQuery.getResultList();
		return allGames;
	}

}
